package com.peaksport.pkms.permit.controller;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;
import javax.ws.rs.core.MediaType;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.peaksport.framework.base.controller.BaseController;
import com.peaksport.framework.uuid.PKUuid;
import com.peaksport.framework.vo.SessionInfo;
import com.peaksport.pkms.permit.entity.UserEntity;
import com.peaksport.pkms.permit.service.SSOService;

@RestSchema(schemaId ="pkms-permit-sso")
@RequestMapping(value="sso", produces = MediaType.APPLICATION_JSON)

public class SSOController extends BaseController{
	
	@Autowired
	private SSOService loginDelegate;
	

	@GetMapping(value="login")
	public SessionInfo login(@RequestParam(required=true) String userNum,
						@RequestParam(required=true) String password) {
		UserEntity userEntity = loginDelegate.login(userNum, password);
		SessionInfo contextInfo = new SessionInfo();
		contextInfo.setSessionId(PKUuid.create(SessionInfo.uuidType).toString());
		contextInfo.setUserEntity(userEntity);
		loginDelegate.setSessionInfo(contextInfo);
		return contextInfo;
	}

	@GetMapping(value="checkFuncPerm")
	@Transactional
	public Map<String,Boolean> checkFuncPerm(@RequestParam(required=true) String userId,
								@RequestParam(required=true) String orgId,
								@RequestParam(required=true) String permItemId) {

		boolean hasFuncPerm = loginDelegate.checkFuncPerm(userId, orgId, permItemId);
		Map<String,Boolean> resultData = new HashMap<String,Boolean>();
		resultData.put("hasFuncPerm", hasFuncPerm);
		return resultData;
		
	}
}
